function loadPlaylist(client_id, playlist_id) {
    $.ajax({
        url: base_url + "chapter/ajaxlist",
        data: "client_id=" + client_id + '&playlist_id=' + playlist_id,
        type: "POST",
        success: function(result) {
            $('#playlist').html(result);
        }
    });
}

function loadMPos(module, layout_id, chapter_id, playlist_id) {
    data_img = $('#layout option:selected').attr('data-img');
    img_tag = '';
    if (data_img) {
        img_tag = '<img height=200px src="' + upload_path + '/templates/' + data_img + '" />';
    }
    $("#thumb").html(img_tag);
    $.ajax({
        url: module + "/loadmpos",
        data: 'layout_id=' + layout_id + '&chapter_id=' + chapter_id + '&playlist_id=' + playlist_id,
        type: "POST",
        success: function(result) {
            $('#layout_pos').html(result);
        }
    });
}


function loadBg(tabindex) {
    data_img = $('#tabs-'+tabindex).find('#background').find('option:selected').attr('data-img');
    img_tag = '';
    if (data_img) {
        img_tag = '<img src="' + upload_path + '/opparts/B_GROUND/' + data_img + '" height="100"/>';
    }
    $('#tabs-'+tabindex).find('#bg_preview').html(img_tag);
}

function loadPos(layout_id, pos_id) {
    $.ajax({
        url: base_url + "chapter/tchapter/ajaxlist",
        data: "pos_id=" + pos_id + '&layout_id=' + layout_id,
        type: "POST",
        success: function(result) {
            $('#layout_pos').html(result);
        }
    });
}


function loadPlaylistByClient(field) {
    client_id = field.value;
    $.ajax({
        url: base_url + "client/listplaylist",
        data: "client_id=" + client_id,
        type: "POST",
        success: function(result) {
            $('#playlist').html(result);
        }
    });
}

function load_parts_ctg(tabindex,parts_type,parts_ctg_key) {
 if(parts_type==1){
        $('#tabs-'+tabindex).find('#parts_ctg_area').hide();
        $('#tabs-'+tabindex).find('#text_area').show();
     }
     else if(parts_type>1){
    $('#tabs-'+tabindex).find('#text_area').hide();
    $('#tabs-'+tabindex).find('#parts_ctg_area').show();
    html = '';
    html+= '<option value="">[ Select ]</option>';
    for (var i = 0; i <= parts_ctg[parts_type].length - 1; i++) {
        selected = '';
        part_ctg = parts_ctg[parts_type][i];
        if(part_ctg.key==parts_ctg_key) selected = 'selected="selected"';
        html += '<option '+ selected +' value="' + part_ctg.key + '">' + part_ctg.key + '</option>';
    };

    $('#tabs-'+tabindex).find('#parts_ctg').html(html);
}
}

function load_parts_ctg_t(tabindex,parts_type,parts_ctg_key) {
     if(parts_type==1){
        $('#tabs-'+tabindex).find('#parts_ctg_area').hide();
        $('#tabs-'+tabindex).find('#text_area').show();
     }
     else{
    $('#tabs-'+tabindex).find('#text_area').hide();
    $('#tabs-'+tabindex).find('#parts_ctg_area').show();
    html = '';
    html+= '<option value="">[ Select ]</option>';
    for (var i = 0; i <= parts_ctg[parts_type].length - 1; i++) {
        selected = '';
        part_ctg = parts_ctg[parts_type][i];
        if(part_ctg.key==parts_ctg_key) selected = 'selected="selected"';
        html += '<option '+ selected +' value="' + part_ctg.key + '">' + part_ctg.key + '</option>';
    };

    $('#tabs-'+tabindex).find('#parts_ctg').html(html);
     }

}


function load_parts_file(tabindex,playlist_id,file_id){
    $('#tabs-'+tabindex).find('#parts_file_preview').html('');

    parts_ctg_key = $('#tabs-'+tabindex).find('#parts_ctg').val();
     key_search = $('#tabs-'+tabindex).find('#key_search').val();
        $.ajax({
        url: base_url + "chapter/tchapter/loadPartsFile",
        data: "parts_ctg=" + parts_ctg_key + '&playlist_id=' + playlist_id+ '&file_id=' + file_id+ '&key_search=' + key_search,
        type: "POST",
        success: function(result) {
            $('#tabs-'+tabindex).find('#parts_file_area').show();
            $('#tabs-'+tabindex).find('#parts_file').html(result);
            preview_file(tabindex);
         }
        });
         $('#tabs-'+tabindex).find('#parts_file_area').show();

}

function preview_file(tabindex) {
    data_img = $('#tabs-'+tabindex).find('#parts_file').find('option:selected').attr('data-img');

    if(data_img && is_image(data_img)){
    img_tag = '';
    if (data_img) {
        img_tag = '<img src="' + upload_path + '/' + data_img + '" height="100"/>';
    }
    $('#tabs-'+tabindex).find('#parts_file_preview').html(img_tag);
}
}


function loadMultiPlayList(client_id, monitor_type, monitor_ctg) {
    $.ajax({
        url: base_url + "monitor/listplaylist",
        data: "client_id=" + client_id + "&monitor_type=" + monitor_type + "&monitor_ctg=" + monitor_ctg,
        type: "POST",
        success: function(result) {
            $('#playlist').html(result);
        }
    });
}

function loadMultiPlayList(client_id, monitor_type, monitor_ctg, monitor_id) {
    $.ajax({
        url: base_url + "monitor/listplaylist",
        data: "client_id=" + client_id + "&monitor_type=" + monitor_type + "&monitor_ctg=" + monitor_ctg + "&monitor_id=" + monitor_id,
        type: "POST",
        success: function(result) {
            $('#playlist').html(result);
        }
    });
}

function loadLayouts(layout_ctg,monitor_type) {
    $('#thumb').html('');
    $('#layout_pos').html('');
    $.ajax({
        url: "/chapter/mchapter/loadlayouts",
        data: "layout_ctg=" + layout_ctg +"&monitor_type=" + monitor_type,
        type: "POST",
        success: function(result) {
            $('#layout').html(result);
        }
    });
}

function previewPartsFile(div_select) {
    data_img = $(div_select).find('option:selected').attr('data-file');
    $(div_select).parent().next().html('<img src="' + upload_path + '/parts_file/' + data_img + '" height="100px"/>');
}
function is_image(image_file)
{
var extensions = new Array("jpg","jpeg","gif","png","bmp");

var image_length = image_file.length;

var pos = image_file.lastIndexOf('.') + 1;

var ext = image_file.substring(pos, image_length);

var final_ext = ext.toLowerCase();

for (i = 0; i < extensions.length; i++)
{
    if(extensions[i] == final_ext)
    {
    return true;
    }
}

return false;
}

function clientAutoComplete(clients){
$( "#client_name" ).autocomplete({
      minLength: 0,
      source: clients,
      focus: function( event, ui ) {
        $( "#client_name" ).val( ui.item.value );
        return false;
      },
      select: function( event, ui ) {
        $( "#client_name" ).val( ui.item.value);
        $( "#client_id" ).val( ui.item.id);
        return false;
      },
       change: function(event, ui) {
            if(ui.item==null){
               $( "#client_name" ).val('');
            }
       }
    })
    .data( "ui-autocomplete" )._renderItem = function( ul, item ) {
      return $( "<li>" )
        .append( "<a>" + item.value + "</a>" )
        .appendTo( ul );
    };
}
